package com.fundboss.display;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.market.MarketConfig;

public class TradersParamPanel extends JPanel{

	private static final String INVESTORS_MANY = "Many";
	private static final String INVESTORS_FEW = "Few";
	private static final String INVESTORS_ONE = "One";
	private static final String INVESTORS_NONE = "None";
	private static final String TS_EMH_IMMEDIATE_DEMAND_CURVE_EXEC = "EMH (Immediate demand curve exec.)";
	private static final String TS_LIQUIDITY_DEMAND_ADJ_LIMITS = "Liquidity & demand adj. limits";
	
	

	public TradersParamPanel(final MarketConfig config,final TraderConfig traders){
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.weightx=1;
		constraints.weighty=0;
		//constraints.ipadx=8;
		//constraints.ipady=8;
		//constraints.insets=new Insets(5,5,5,5);
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		
		JLabel title = new JLabel("Traders");
		title.setFont(new Font("Arial",Font.BOLD,18));
		add(title,constraints);
		
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(new JLabel(""),constraints);
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Investors"),constraints);
		final JComboBox investors = new JComboBox(new Object[]{INVESTORS_NONE,INVESTORS_ONE,INVESTORS_FEW,INVESTORS_MANY});
		investors.setSelectedItem(INVESTORS_ONE);
		traders.setNumberOfInvestors(TraderConfig.INVESTORS_ONE);
		
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(investors,constraints);
		investors.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String option = (String) investors.getSelectedItem();
				if(option.equals(INVESTORS_NONE)){
					traders.setNumberOfInvestors(TraderConfig.INVESTORS_NONE);
				}else if(option.equals(INVESTORS_ONE)){
					traders.setNumberOfInvestors(TraderConfig.INVESTORS_ONE);
				}else if(option.equals(INVESTORS_FEW)){
					traders.setNumberOfInvestors(TraderConfig.INVESTORS_FEW);
				}else if(option.equals(INVESTORS_MANY)){
					traders.setNumberOfInvestors(TraderConfig.INVESTORS_MANY);
				}else{
					throw new RuntimeException("Unexpected investors: "+option);
				}
			}
		});
		
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Trade Strategy"),constraints);
		final JComboBox tradeStrategy = new JComboBox(new Object[]{
														TS_EMH_IMMEDIATE_DEMAND_CURVE_EXEC,
														TS_LIQUIDITY_DEMAND_ADJ_LIMITS});
		tradeStrategy.setSelectedItem(TS_EMH_IMMEDIATE_DEMAND_CURVE_EXEC);
		config.setTradeStrategy(MarketConfig.TRADE_STRAT_EMH);
		//add(new JLabel());//
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(tradeStrategy,constraints);
		tradeStrategy.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String option = (String) tradeStrategy.getSelectedItem();
				if(option.equals(TS_EMH_IMMEDIATE_DEMAND_CURVE_EXEC)){
					config.setTradeStrategy(MarketConfig.TRADE_STRAT_EMH);
				}else if(option.equals(TS_LIQUIDITY_DEMAND_ADJ_LIMITS)){
					config.setTradeStrategy(MarketConfig.TRADE_STRAT_ADJ);
				}else{
					throw new RuntimeException("Unexpected trade strategy: "+option);
				}
			}
		});
		
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Market maker liquidity"),constraints);
		final JComboBox liquidity = new JComboBox(new Object[]{"Low","Medium","High","Very High"});
		liquidity.setSelectedItem("Medium");
		config.setLiquidity(600);
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(liquidity,constraints);
		liquidity.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String option = (String) liquidity.getSelectedItem();
				if(option.equals("Low")){
					config.setLiquidity(200);
				}else if(option.equals("Medium")){
					config.setLiquidity(1000);
				}else if(option.equals("High")){
					config.setLiquidity(3000);
				}else if(option.equals("Very High")){
					config.setLiquidity(20000);
				}else{
					throw new RuntimeException("Unexpected liquidity: "+option);
				}
			}
		});
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Appetite"),constraints);
		final JComboBox appetite = new JComboBox(new Object[]{
														MarketConfig.APPETITE_FIXED,
														MarketConfig.APPETITE_SHARPE_RELATED,
														MarketConfig.APPETITE_STD_DEV});
		appetite.setSelectedItem(MarketConfig.APPETITE_SHARPE_RELATED);
		config.setAppetite(MarketConfig.APPETITE_SHARPE_RELATED);
		//add(new JLabel());//
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(appetite,constraints);
		appetite.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String option = (String) appetite.getSelectedItem();
				config.setAppetite(option);
			}
		});
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Leverage Multiplier (for sharpe related appetite)"),constraints);
		final JComboBox sharpeMultiplier = new JComboBox(new Object[]{
														MarketConfig.SharpeMultiplier.LOW_LEVERAGE,
														MarketConfig.SharpeMultiplier.MEDIUM_LEVERAGE,
														MarketConfig.SharpeMultiplier.HIGH_LEVERAGE,
														MarketConfig.SharpeMultiplier.VERY_HIGH_LEVERAGE,
														MarketConfig.SharpeMultiplier.SUPER_LEVERAGE,
														MarketConfig.SharpeMultiplier.SUPER_PLUS_LEVERAGE,
														//MarketConfig.SharpeMultiplier.SUPER_PLUS_2_LEVERAGE,
														//MarketConfig.SharpeMultiplier.SUPER_DUPER_LEVERAGE
														});
		sharpeMultiplier.setSelectedItem(MarketConfig.SharpeMultiplier.MEDIUM_LEVERAGE);
		config.setSharpeMultiplier(MarketConfig.SharpeMultiplier.MEDIUM_LEVERAGE);
		//add(new JLabel());//
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(sharpeMultiplier,constraints);
		sharpeMultiplier.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				MarketConfig.SharpeMultiplier option = (MarketConfig.SharpeMultiplier) sharpeMultiplier.getSelectedItem();
				
				config.setSharpeMultiplier(option);
			}
		});
		
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Include Index Tracker"),constraints);
		
		traders.setIncludeIndexTracker(false);
		
		final JCheckBox checkBox = new JCheckBox();
		//add(new JLabel());//
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(checkBox,constraints);
		checkBox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				traders.setIncludeIndexTracker(checkBox.isSelected());
			}
		});
		
		constraints.gridwidth=GridBagConstraints.RELATIVE;
		add(new JLabel("Include Momentum Trader"),constraints);
		
		traders.setIncludeMomentumTrader(false);
		
		final JCheckBox checkBox2 = new JCheckBox();
		//add(new JLabel());//
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(checkBox2,constraints);
		checkBox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				traders.setIncludeMomentumTrader(checkBox2.isSelected());
			}
		});
		
		
		constraints.weighty=1;
		constraints.gridheight=GridBagConstraints.REMAINDER;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		add(new JLabel(""),constraints);
		
	}
}
